﻿#include "borderpainter.h"

BorderPainter::BorderPainter(QObject* parent)
	: Painter(parent)
{

}

void BorderPainter::paint(QPainter& painter)
{
	int width = final_point.x() - start_point.x();
	int height = final_point.y() - start_point.y();
	//默认蓝色外圈
	painter.setPen(QPen(QColor(6, 123, 239)));
	//默认白色内圈
	painter.setBrush(QBrush(QColor(255, 255, 255)));
	//左上
	painter.drawRect(start_point.x() - border_length, start_point.y() - border_length, border_length * 2, border_length * 2);
	//左中
	painter.drawRect(start_point.x() - border_length, start_point.y() - border_length + height / 2, border_length * 2, border_length * 2);
	//左下
	painter.drawRect(start_point.x() - border_length, start_point.y() - border_length + height, border_length * 2, border_length * 2);
	//中上
	painter.drawRect(start_point.x() - border_length + width / 2, start_point.y() - border_length, border_length * 2, border_length * 2);
	//中下
	painter.drawRect(start_point.x() - border_length + width / 2, start_point.y() - border_length + height, border_length * 2, border_length * 2);
	//右上
	painter.drawRect(start_point.x() - border_length + width, start_point.y() - border_length, border_length * 2, border_length * 2);
	//右中
	painter.drawRect(start_point.x() - border_length + width, start_point.y() - border_length + height / 2, border_length * 2, border_length * 2);
	//右下
	painter.drawRect(start_point.x() - border_length + width, start_point.y() - border_length + height, border_length * 2, border_length * 2);
	//绘制虚线边框
	painter.setPen(QPen(QColor(6, 123, 239), 1, Qt::DashLine));
	painter.drawLine(start_point.x() + border_length, start_point.y(), start_point.x() + width / 2 - border_length, start_point.y());
	painter.drawLine(start_point.x() + width / 2 + border_length, start_point.y(), start_point.x() + width - border_length, start_point.y());
	painter.drawLine(start_point.x(), start_point.y() + border_length, start_point.x(), start_point.y() + height / 2 - border_length);
	painter.drawLine(start_point.x(), start_point.y() + height / 2 + border_length, start_point.x(), start_point.y() + height - border_length);
	painter.drawLine(start_point.x() + width, start_point.y() + border_length, start_point.x() + width, start_point.y() + height / 2 - border_length);
	painter.drawLine(start_point.x() + width, start_point.y() + height / 2 + border_length, start_point.x() + width, start_point.y() + height - border_length);
	painter.drawLine(start_point.x() + border_length, start_point.y() + height, start_point.x() + width / 2 - border_length, start_point.y() + height);
	painter.drawLine(start_point.x() + width / 2 + border_length, start_point.y() + height, start_point.x() + width - border_length, start_point.y() + height);
}

GirdPosition BorderPainter::BorderDetect(const QPoint& point) const
{
	int width = final_point.x() - start_point.x();
	int height = final_point.y() - start_point.y();
	if (QRect(start_point.x() - border_length, start_point.y() - border_length, border_length * 2, border_length * 2).contains(point))
		return GirdPosition::LeftTop;
	if (QRect(start_point.x() - border_length, start_point.y() - border_length + height / 2, border_length * 2, border_length * 2).contains(point))
		return GirdPosition::LeftMid;
	if (QRect(start_point.x() - border_length, start_point.y() - border_length + height, border_length * 2, border_length * 2).contains(point))
		return GirdPosition::LeftEnd;
	if (QRect(start_point.x() - border_length + width / 2, start_point.y() - border_length, border_length * 2, border_length * 2).contains(point))
		return GirdPosition::MainTop;
	if (QRect(start_point.x() - border_length + width / 2, start_point.y() - border_length + height, border_length * 2, border_length * 2).contains(point))
		return GirdPosition::MainEnd;
	if (QRect(start_point.x() - border_length + width, start_point.y() - border_length, border_length * 2, border_length * 2).contains(point))
		return GirdPosition::RightTop;
	if (QRect(start_point.x() - border_length + width, start_point.y() - border_length + height / 2, border_length * 2, border_length * 2).contains(point))
		return GirdPosition::RightMid;
	if (QRect(start_point.x() - border_length + width, start_point.y() - border_length + height, border_length * 2, border_length * 2).contains(point))
		return GirdPosition::RightEnd;
	return GirdPosition::Missing;
}

SelectPainter::SelectPainter(QObject* parent)
	: Painter(parent)
{
}

void SelectPainter::paint(QPainter& painter)
{
	painter.setPen(QPen(QColor(110, 177, 235)));
	painter.setBrush(QBrush(QColor(231, 244, 253)));
	painter.drawRect(start_point.x(), start_point.y(), final_point.x() - start_point.x(), final_point.y() - start_point.y());
}

ConnectPainter::ConnectPainter(QObject* parent, ShapeManager* manager)
	: Painter(parent)
{
	shape_manager = manager;
}

void ConnectPainter::paint(QPainter& painter)
{
	painter.setPen(QPen(QColor(50, 50, 50)));
	painter.setBrush(QBrush(QColor(50, 50, 50)));
	for (auto link : shape_manager->linkedGroup())
	{
		QPoint s_point;
		QPoint f_point = link->getFinalPoint();
		if (link->getFinalShape() == nullptr)
		{
			link->getStartShape()->getNodePoint(s_point, link->getStartIndex());
		}
		else
		{
			link->getStartShape()->getNodePoint(s_point, link->getStartIndex());
			link->getFinalShape()->getNodePoint(f_point, link->getFinalIndex());
		}
		painter.drawLine(s_point, f_point);
	}
}
